﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Inspire.Main.UIElements
{
    class NavStackPanel : StackPanel, INavHost
    {
        // INavHost NavHostParent
        private INavHost NavHostParent;

        // List of Expanders
        private List<NavExpander> Submenus = new List<NavExpander>();

        /// <summary>
        /// Constructor for Main Nav Panel. Must be contained in a Grid.
        /// </summary>
        /// <param name="column">Column of the grid to place this panel in.</param>
        /// <param name="row">Row of the grid to place this panel in.</param>
        /// <param name="parent">INavHost parent to propogate URI navigation requests to.</param>
        public NavStackPanel(int column, int row, INavHost parent)
        {
            // Set INavHost NavHostParent
            this.NavHostParent = parent;

            // Set Orientation
            this.Orientation = Orientation.Vertical;

            // Set Background Opacity and Color
            SolidColorBrush bgBrush = new SolidColorBrush(Color.FromArgb(24, 0, 0, 0));
            this.Background = bgBrush;

            // Set Grid Column and Row
            Grid.SetColumn(this, column);
            Grid.SetRow(this, row);
        }

        /// <summary>
        /// Add another NavExpander to this StackPanel. The first one added will be expanded, by default.
        /// </summary>
        /// <param name="newExpander">NavExpander to be added.</param>
        public void AddNavExpander(NavExpander newExpander)
        {
            if (this.Submenus.Count <= 0)
            {
                newExpander.IsExpanded = true;
            }
            else
            {
                newExpander.IsExpanded = false;
            }
            this.Submenus.Add(newExpander);
            this.Children.Add(newExpander);
        }

        /// <summary>
        /// Override PropogateNavUp from implementing INavHost.
        /// </summary>
        /// <param name="navigationUri">URI to propogate to the parent of this panel for navigation.</param>
        public void PropogateNavUp(string navigationUri)
        {
            // Determine if a button in the Navigation Panel matches the URI
            // the user is going to via a shortcut. Collapse all menus that don't have
            // that button and expand the one that does.
            foreach (NavExpander nextExpander in this.Submenus)
            {
                if (nextExpander.HasUri(navigationUri))
                {
                    nextExpander.IsExpanded = true;
                    nextExpander.PropogateNavDown(navigationUri);
                }
                else
                {
                    nextExpander.IsExpanded = false;
                    nextExpander.PropogateNavDown(navigationUri);
                }
            }

            // Propogate up to parent
            this.NavHostParent.PropogateNavUp(navigationUri);
        }

        /// <summary>
        /// Override PropogateNavDown for implementing INavHost.
        /// </summary>
        /// <param name="navigationUri"></param>
        public void PropogateNavDown(string navigationUri)
        {
            // Determine if a button in the Navigation Panel matches the URI
            // the user is going to via a shortcut. Collapse all menus that don't have
            // that button and expand the one that does.
            foreach (NavExpander nextExpander in this.Submenus)
            {
                if (nextExpander.HasUri(navigationUri))
                {
                    nextExpander.IsExpanded = true;
                    nextExpander.PropogateNavDown(navigationUri);
                }
                else
                {
                    nextExpander.IsExpanded = false;
                    nextExpander.PropogateNavDown(navigationUri);
                }
            }
        }
    }
}
